Grid = function(target, id, options) 
{
	this.target = target;
	this.id = id;
	window['grid'+id] = this;
		
	this.selectableRows = options.selectableRows || false;
	this.onReady = options.onReady || function() {};
	this.brief = (options.brief ? 1:0) || 0;
	this.filter = options.filter || {};
	this.pagesCount = 1;
	
	this.currPage = 1;
	this.sort = {};
	this.search = '';
	this.recs_on_page = parseInt(50);
	
	var grid = this;
	call('session.get', { name:'grid_'+this.id } ).done( function(params) {
		if (typeof(params) == 'object') {
			if ('currPage' in params) {
				grid.currPage = params.currPage;
			}
			if ('sort' in params) {
				grid.sort = params.sort;
			}
			if ('search' in params) {
				grid.search = params.search;
			}
			if ('recs_on_page' in params) {
				grid.recs_on_page = params.recs_on_page;
			}
		}
		grid.go(grid.currPage);
	});
}

Grid.prototype.onSelect = function(id,select)
{
}

Grid.prototype.onSelectAll = function(select)
{
}

Grid.prototype.onClick = function(id, oTD)
{
}

Grid.prototype.onDblClick = function(id)
{
}

Grid.prototype.onColClick = function(fname)
{
	var d = (fname in this.sort && this.sort[fname]=='a') ? 'd' : 'a';
	this.sort = {};
	this.sort[fname] = d;
	this.refresh();
}

Grid.prototype.go = function(page)
{
	this.currPage = page || 1;
	
	var grid = this;
	
	$.when(
		call('admin.getView?id='+this.id),
		call('admin.getListLength?id='+this.id, {sort:this.sort, search:this.search, filter:this.filter}),
		call('admin.getList?id='+this.id, {
			skip: (this.currPage-1)*grid.recs_on_page, 
			limit: grid.recs_on_page, 
			sort: this.sort, 
			search: this.search,
			filter: this.filter,
			brief: this.brief,
			addHeader: 1
		})
	).done( function (data1, data2, data3) { 
		var recCount = parseInt(data2[0]);
		grid.pagesCount = Math.ceil(recCount/grid.recs_on_page);
		grid.view = data1[0];
		var data = {
			id: grid.id,
			selectableRows: grid.selectableRows,
			count: recCount,
			from: (grid.currPage-1)*grid.recs_on_page+1,
			to: Math.min((grid.currPage-1)*grid.recs_on_page+grid.recs_on_page, recCount),
			pagesCount: grid.pagesCount,
			currPage: grid.currPage,
			columns: data3[0][0],
			sort: grid.sort,
			records: data3[0][1]
		};
		
		var tpl = new jSmart($('#grid_tpl').html());
		grid.target.html( tpl.fetch(data) );
		
		grid.target.find('.grid-search input').val(grid.search).focus();
		
		grid.registerEvents();
		grid.setStyles();
		grid.loadImages();
		
		grid.onReady(grid);
		
		call(
			'session.set', { 
				name:'grid_'+grid.id, 
				value:{
					currPage: grid.currPage,
					sort: grid.sort,
					search: grid.search,
					recs_on_page: grid.recs_on_page
				} 
			} 
		);
	});
}

Grid.prototype.refresh = function()
{
	this.go();
}

Grid.prototype.getLength = function()
{
	return $('#'+this.id+' tbody tr').length;
}

Grid.prototype.getSelected = function() 
{
	var ids = [];
	var checked = $('#'+this.id+' tbody tr td:first-child :checked').each(function() { ids.push($(this).val()) } );
	return ids;
}




Grid.prototype.registerEvents = function() 
{
	var grid = this;
	this.target.find('.grid tbody td:not(.select)').on( {
		'click': function(e){ grid.onEventClick(e); },
		'dblclick': function(e){ grid.onEventDblClick(e); }
	} );
	
	this.target.find('.grid tbody td.select input').on( {
		'click': function(e){ grid.onEventSelect(e); }
	} );
	
	this.target.find('.grid thead td input.selectAll').on( {
		'click': function(e){ grid.onEventSelectAll(e); }
	} );
	
	this.target.find('.grid .navigation select').on( {
		'change': function(e){ 
			grid.recs_on_page = parseInt($(e.target).val());
			grid.go();
		}
	} );
}

Grid.prototype.onEventSelect = function(e)
{
	$(e.target).closest('tr').toggleClass('selected',$(e.target).prop('checked'));
	$(this).closest('table').find('.selectAll').prop(
		'checked',
		this.getLength()==this.getSelected().length
	);
	this.onSelect($(e.target).val(), $(e.target).prop('checked'));
}

Grid.prototype.onEventSelectAll = function(e)
{
	$(e.target).closest('.grid').find('tbody tr td:first-child :checkbox').prop('checked', $(e.target).prop('checked'));
	$(e.target).closest('.grid').find('tbody tr').toggleClass('selected',$(e.target).prop('checked'))
	this.onSelectAll($(e.target).prop('checked'));
}

Grid.prototype.onEventClick = function(e)
{
	this.onClick($(e.target).closest('tr').attr('id'), $(e.target));
}

Grid.prototype.onEventDblClick = function(e)
{
	this.onDblClick($(e.target).closest('tr').attr('id'));
}


Grid.prototype.setStyles = function()
{
	this.target.find('.navigation .grid-first').button({
		text: false,
		icons: {
			primary: "ui-icon-seek-first"
		}
	});
	this.target.find('.navigation .grid-prev').button({
		text: false,
		icons: {
			primary: "ui-icon-seek-prev"
		}
	});
	this.target.find('.navigation .grid-first, .grid-prev').button( (this.currPage>1)?'enable':'disable');

	
	this.target.find('.navigation .grid-next').button({		
		text: false,
		icons: {
			primary: "ui-icon-seek-next"
		}
	});
	this.target.find('.navigation .grid-last').button({		
		text: false,
		icons: {
			primary: "ui-icon-seek-end"
		}
	});
	
	this.target.find('.navigation .grid-next, .grid-last').button( (this.currPage<this.pagesCount)?'enable':'disable');
	
//	this.target.find('.navigation a').css('width','20px').css('height','20px');

	this.target.find('.navigation select').val(this.recs_on_page);

	var grid = this;
	this.target.find('.grid-search').on( {
		'submit' : function(e) { 
			e.preventDefault();
			grid.search = $('.grid-search input').val();
			grid.refresh();
		}
	} );
}

Grid.prototype.loadImages = function()
{
	$(this.target).find('.grid-col-image img').each( function() {
		var image = $(this);
		call('image.getThumbnail',{ path: image.attr('path'), width:50, height:50 } ).done( function (thumb) {
			image.attr('src', thumb);
		} );
	} );
}